# -*-coding:utf-8-*-


import os
import time
from ftplib import FTP
import xlrd

# 服务器地址
FTP_SERVER = '172.18.6.2'
#ftpe服务器地址
USER = 'adj'
PWD = 'adjftp'
FTP_PATH = '/'
local_root = 'e:/mypiv1' + FTP_PATH
DATE = time.strftime('%Y%m%d', time.localtime(time.time()))

def isDir(filename):
    try:
        path = filename;
        path.replace('/', '\\')
        if os.path.exists(path):
            print('---file exists--')
        else:
            print('file not exists ', local_root)
            os.mkdirs(local_root)
        return True
    except:
        return False


def ftpconnect():
    ftp = FTP()
    ftp.set_debuglevel(2)
    ftp.connect(FTP_SERVER, 21)
    ftp.login(USER, PWD)
    return ftp


def downloadfile():
    ftp = ftpconnect()
    print(ftp.getwelcome())  # 显示ftp服务器欢迎信息

    li = ftp.nlst(FTP_PATH)
    all_log = open("E:/mypiv1/all.txt","wb+")
    print('ftp: ', li)
    for eachfile in li:
        localpath = 'E:\mypiv1' + eachfile
        bufsize = 1024
        #isDir(localpath)

        if not os.path.exists(localpath):
            fp = open(localpath, 'wb+')
            print('-- open localpath --', localpath)
            ftp.retrbinary('RETR ' + eachfile, fp.write, bufsize)
            ftp.retrbinary('RETR ' + eachfile, all_log.write, bufsize)
            all_log.flush()
            fp.flush()
            fp.close()


    ftp.set_debuglevel(0)  # 关闭调试
    all_log.close()
    ftp.quit()  # 退出ftp服务器


if __name__ == "__main__":
    downloadfile()
    sssss = '';
